﻿using System;
using Autodesk.Revit.DB;
using YJKGeometryFuncs;

namespace YJKArchBaseCode.OutWallSearch
{
	public class OverlapFinder
	{
		public static Geometry.OverlapType OverlapFind(EdgeInfo edgeInfo1, EdgeInfo edgeInfo2, ref XYZ ptStart, ref XYZ ptEnd)
		{
			Geometry.OverlapType result = Geometry.OverlapType._OT_None;
			if (edgeInfo1.IsArc != edgeInfo2.IsArc)
			{
				return result;
			}
			if (edgeInfo1.IsArc)
			{
				EdgeInfo formatArcEdgeInfo = OverlapFinder.GetFormatArcEdgeInfo(edgeInfo1);
				EdgeInfo formatArcEdgeInfo2 = OverlapFinder.GetFormatArcEdgeInfo(edgeInfo2);
				result = Geometry.IsOverlapOfTwoArc(OverlapFinder.Point2d(formatArcEdgeInfo.StartPoint), OverlapFinder.Point2d(formatArcEdgeInfo.EndPoint), OverlapFinder.Point2d(formatArcEdgeInfo.Center), formatArcEdgeInfo.Normal, OverlapFinder.Point2d(formatArcEdgeInfo2.StartPoint), OverlapFinder.Point2d(formatArcEdgeInfo2.EndPoint), OverlapFinder.Point2d(formatArcEdgeInfo2.Center), formatArcEdgeInfo2.Normal, ref ptStart, ref ptEnd, OverlapFinder.m_Tolerance);
			}
			else
			{
				result = Geometry.IsOverlapOfTwoLine(OverlapFinder.Point2d(edgeInfo1.StartPoint), OverlapFinder.Point2d(edgeInfo1.EndPoint), OverlapFinder.Point2d(edgeInfo2.StartPoint), OverlapFinder.Point2d(edgeInfo2.EndPoint), ref ptStart, ref ptEnd, true, OverlapFinder.m_Tolerance, OverlapFinder.m_Tolerance);
			}
			return result;
		}

		private static EdgeInfo GetFormatArcEdgeInfo(EdgeInfo edgeInfo)
		{
			if (!edgeInfo.IsArc)
			{
				return edgeInfo;
			}
			if (Geometry.GreaterThan(edgeInfo.Bulge, 0.0))
			{
				return edgeInfo.ReverseTo();
			}
			return edgeInfo;
		}

		public static XYZ Point2d(XYZ pt)
		{
			return new XYZ(pt.X, pt.Y, 0.0);
		}

		public static double m_Tolerance = 0.01;
	}
}
